<?php

namespace app\admin\service;

use app\admin\model\BoxAdMedia;
use app\admin\model\BoxDataOverview;
use app\admin\model\BoxGame;
use app\admin\model\BoxMemberRemainLog;
use app\admin\model\BoxStatisticsAdOverviewLog;
use app\admin\model\BoxStatisticsAdTrackLog;
use app\admin\model\Package;
use app\admin\model\UserPromote;

class BoxStatisticsAdTrackService extends BaseService
{

    /**
     * @Notes:广告投放统计
     * @Interface getList
     * @return array
     */
    public function getList()
    {
        $param = request()->param();

        $map = [];

        if (!empty($param['time_range'])) {
            $param['time_range'][0] = implode('', explode('-', $param['time_range'][0]));
            $param['time_range'][1] = implode('', explode('-', $param['time_range'][1]));
            $map[]                  = ['day_time', '>=', $param['time_range'][0]];
            $map[]                  = ['day_time', '<=', $param['time_range'][1]];
        }

        if (isset($param['media_id']) && !empty($param['media_id'])) {
            $map[] = ['media_id', '=', $param['media_id']];
        }

        if (isset($param['promote_id']) && !empty($param['promote_id'])) {
            $map[] = ['promote_id', '=', $param['promote_id']];
        }

        if (isset($param['platform']) && !empty($param['platform'])) {
            $map[] = ['platform', '=', $param['platform']];
        }

        $list = (new BoxStatisticsAdTrackLog())
            ->field('day_time,platform,promote_id,package_id,media_id,box_register_num,box_game_num,box_role_num,game_register_num,role_num,cost')
            ->where($map)
            ->order('day_time', 'desc')
            ->select()->toArray();
        //获取数据总数
        $count = count($list);

        //媒体
        $promoteData = UserPromote::field(['id', 'realname'])->select()->toArray();
        $promoteData = array_column($promoteData, 'realname', 'id');

        //产品
        $packageData = Package::field(['id', 'package_name'])->select()->toArray();
        $packageData = array_column($packageData, 'package_name', 'id');
        $packageData['9999997'] = '千年游戏盒子pc';
        $packageData['9999998'] = '千年游戏盒子ios';
        $packageData['9999999'] = '千年游戏盒子android';

        //投放渠道
        $mediaData = BoxAdMedia::field(['id', 'name'])->select()->toArray();
        $mediaData = array_column($mediaData, 'name', 'id');

        if (!empty($list)) {
            $box_register_num_all        = 0;
            $box_game_num_all            = 0;
            $game_register_rate_all      = 0;
            $box_role_num_all            = 0;
            $game_total_register_num_all = 0;
            $game_register_num_all       = 0;
            $role_num_all                = 0;
            $cost_all                    = 0;
            foreach ($list as $key => &$item) {
                $box_register_num_all        += $item['box_register_num'];
                $box_game_num_all            += $item['box_game_num'];
                $game_register_rate_all      += empty($item['box_register_num']) ? '0' : round($item['box_game_num'] / $item['box_register_num'] * 100, 2) ?? '0';
                $box_role_num_all            += $item['box_role_num'];
                $game_total_register_num_all += empty($item['box_register_num']) ? '0' : round($item['box_role_num'] / $item['box_register_num'] * 100, 2) ?? '0';
                $game_register_num_all       += $item['game_register_num'];
                $role_num_all                += $item['role_num'];
                $cost_all                    += $item['cost'];

                $item['day_time']                = date("Y-m-d", strtotime($item['day_time']));
                $item['media_name']              = $mediaData[$item['media_id']] ?? '';
                $item['promote_name']            = $promoteData[$item['promote_id']] ?? '';
                $item['package_name']            = $packageData[$item['package_id']] ?? '';
                $item['game_register_rate']      = empty($item['box_register_num']) ? '0%' : round($item['box_game_num'] / $item['box_register_num'] * 100, 2) . "%";
                $item['game_total_register_num'] = empty($item['box_register_num']) ? '0%' : round($item['box_role_num'] / $item['box_register_num'] * 100, 2) . "%";

            }
            $list[$count]['day_time']                = "汇总";
            $list[$count]['media_name']              = "-";
            $list[$count]['package_name']            = "-";
            $list[$count]['platform']                = "-";
            $list[$count]['promote_name']            = "-";
            $list[$count]['box_register_num']        = $box_register_num_all;
            $list[$count]['box_game_num']            = $box_game_num_all;
            $list[$count]['game_register_rate']      = round($game_register_rate_all / $count, 2) . "%";
            $list[$count]['box_role_num']            = $box_role_num_all;
            $list[$count]['game_total_register_num'] = round($game_total_register_num_all / $count, 2) . "%";
            $list[$count]['game_register_num']       = $game_register_num_all;
            $list[$count]['role_num']                = $role_num_all;
            $list[$count]['cost']                    = $cost_all;
        }
        //返回结果
        return [
            "msg"   => '操作成功',
            "code"  => 0,
            "data"  => $list,
            "count" => $count,
        ];
    }

}
